
// 新增
$(function () {
    $("#save-casegroup-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#addcasegroup-dialog");
        //获取元素
        var groupnameE = $("input[name=groupname]");
        var groupname = groupnameE.val();
        var groupcodeE = $("input[name=groupcode]");
        var groupcode = groupcodeE.val();

        var submitType = self.attr("data-type"); //编辑的时候动态绑定该属性
        var group_id = self.attr("data-id"); //编辑的时候动态绑定该属性

        if(!groupname || !groupcode){
            amanalert.alertInfo("分组名称或分组编码不能为空！");
            return;
        }

        var url = '';
        if(submitType == 'update'){
            url = '/updatecasegroup/';
            res_message = '分组修改成功！'
        }else {
            url = '/addcasegroup/';
            res_message = '分组新增成功！'
        }
        amanajax.post({
            'url':url,
            'data':{
                'groupname':groupname,
                'groupcode':groupcode,
                'group_id':group_id
            },
            'success':function (data) {
                if(data['code'] == 1){
                    dialog.modal('hide');
                    amanalert.alertSuccessToast(res_message);
                    window.location.reload();
                }else {
                    amanalert.alertInfo(data['message']);
                }
            },
            'fail':function (error) {
                // console.log(error);
                amanalert.alertNetworkError()
            }
        });
    });
});

// 编辑
$(function () {
    $(".edit-casegroup-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#addcasegroup-dialog");

        dialog.modal("show");
        var tr = self.parent().parent();

        var groupname = tr.attr("data-group_name");
        var groupnameE = dialog.find("input[name='groupname']");
        var groupcode = tr.attr("data-group_code");
        var groupcodeE = dialog.find("input[name='groupcode']");
        groupcodeE.attr("disabled", "disabled");
        var saveBtn = dialog.find("#save-casegroup-btn");

        groupnameE.val(groupname);
        groupcodeE.val(groupcode);

        saveBtn.attr("data-type", "update"); // 给保存按钮绑定一个data-type属性，值为update
        saveBtn.attr("data-id", tr.attr("data-id"));

    });
});

// 启用/停用
$(function () {
    $(".status-casegroup-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var tr = self.parent().parent();
        var group_id = tr.attr('data-id');
        var statusText = this.value;
        amanajax.post({
            url: "/updatestatus_gp/",
            data:{
                'group_id':group_id,
                'status':statusText
            },
            'success':function (data) {
                if(data['code'] == 1){
                    window.location.reload();
                }else {
                    amanalert.alertConfirm({
                        "msg":data['message'],
                        "confirmCallback":function () {
                            window.location.reload();
                        },
                        "cancelCallback":function () {
                            window.location.reload();
                        }
                    });
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError();
            }
        });
    });
});

// 删除
$(function () {
    $(".delete-casegroup-btn").click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var group_id = tr.attr('data-id');
        amanalert.alertConfirm({
            "msg":"确定删除这个分组吗？",
            "confirmCallback":function () {
                amanajax.post({
                    'url':'/delcasegroup/',
                    'data':{
                        'group_id':group_id
                    },
                    'success':function (data) {
                        if(data['code'] == 1){
                            amanalert.alertSuccessToast('删除成功！');
                            window.location.reload();
                        }else {
                            amanalert.alertToast(data['message']);
                            // window.location.reload();
                        }
                    },
                    'fail':function (error) {
                        amanalert.alertNetworkError();
                    }
                })
            }
        });
    });
});